﻿using Hei.Captcha;
using Microsoft.AspNetCore.Http;


namespace CoreHelper
{
    /// <summary>
    /// 图形验证码帮助类,跨平台,使用别人开发的包Hei.Captcha
    /// 需要安装Microsoft.AspNetCore.Session包
    /// </summary>
    public class VerifyCodeHelper2
    {
        #region 变更与属性
        private readonly IHttpContextAccessor _httpContextAccessor = new HttpContextAccessor();
        private ISession _session => _httpContextAccessor.HttpContext.Session;
        //_httpContextAccessor
        //使用SetString和GetString方法需要安装Microsoft.AspNetCore.Http.Extensions包和Microsoft.AspNetCore.Session包,并且需要在
        //_session.SetString("code", "123456");            
        //string code = _session.GetString("code"); 

        /// <summary>
        /// session默认名称
        /// </summary>
        static string _sessionName = "CheckCode";
        #endregion


        #region 泡泡中文验证码
        /// <summary>
        /// 泡泡中文验证码 
        /// </summary>
        /// <returns></returns>
        public byte[] BubbleCode(int length = 2)
        {
            var _securityCode = new SecurityCodeHelper();
            var code = _securityCode.GetRandomCnText(length);
            var imgbyte = _securityCode.GetBubbleCodeByte(code);
            _session.SetString(_sessionName, code.ToLower());
            return imgbyte;
        }
        #endregion

        #region 数字字母组合验证码
        /// <summary>
        /// 数字字母组合验证码
        /// </summary>
        /// <returns></returns>
        public byte[] HybridCode(int length = 4)
        {
            var _securityCode = new SecurityCodeHelper();
            var code = _securityCode.GetRandomEnDigitalText(length);
            var imgbyte = _securityCode.GetEnDigitalCodeByte(code);
            _session.SetString(_sessionName, code.ToLower());
            return imgbyte;
        }
        #endregion

        #region gif泡泡中文验证码
        /// <summary>
        /// gif泡泡中文验证码 
        /// </summary>
        /// <returns></returns>
        public byte[] GifBubbleCode(int length = 2)
        {
            var _securityCode = new SecurityCodeHelper();
            var code = _securityCode.GetRandomCnText(length);
            var imgbyte = _securityCode.GetGifBubbleCodeByte(code);
            _session.SetString(_sessionName, code.ToLower());
            return imgbyte;
        }
        #endregion

        #region gif数字字母组合验证码
        /// <summary>
        /// gif数字字母组合验证码
        /// </summary>
        /// <returns></returns>
        public byte[] GifHybridCode(int length = 4)
        {
            var _securityCode = new SecurityCodeHelper();
            var code = _securityCode.GetRandomEnDigitalText(length);
            var imgbyte = _securityCode.GetGifEnDigitalCodeByte(code);
            _session.SetString(_sessionName, code.ToLower());
            return imgbyte;
        }
        #endregion


        #region 对比验证码
        /// <summary>
        /// 对比验证码
        /// </summary>
        /// <param name="input"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public bool Check(string input, out string error)
        {
            error = "";
            if (string.IsNullOrEmpty(input))
            {
                error = "请输入验证码";
                return false;
            }
            if (_session.GetString(_sessionName) == null)
            {
                error = "验证码未初始化,请刷新重试";
                return false;
            }
            if (_session.GetString(_sessionName) != input.ToLower())
            {
                error = "验证码不正确";
                return false;
            }
            return true;
        }
        #endregion
    }
}
